﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

public partial class PromoterRegister : WebPageBase
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Si es un coordinador puede registrar Lideres.
            LiteralType.Visible = (base.UserSystem.RoleId == 3);
            DropDownListType.Visible = (base.UserSystem.RoleId == 3);
        }
    }

    protected void ButtonPromoter_Click(object sender, EventArgs e)
    {

        Interview interviewPromoter = db.Interviews.Where(u => u.PromoterDNI.Equals(TextBoxPromoterDNI.Text)).FirstOrDefault();
        Interview interviewRepresentative = 
            db.Interviews.Where(u => u.RepresentativeDNI.Equals(TextBoxRepresentativeDNI.Text) && !u.RepresentativeDNI.Equals(String.Empty)).FirstOrDefault();

        if (interviewPromoter == null && interviewRepresentative==null)
        {

            try
            {
                IFormatProvider culture = new System.Globalization.CultureInfo("ES-PE", true);
                DateTime dateBirth = DateTime.Parse(TextBoxDateBirth.Text, culture, System.Globalization.DateTimeStyles.AssumeLocal);

                if (dateBirth > DateTime.Now.AddYears(-18) || DropDownListPromoterTypeDocument.SelectedValue.Equals("CE"))
                {

                    if (String.IsNullOrEmpty(TextBoxRepresentativeName.Text.Trim())     || String.IsNullOrEmpty(TextBoxRepresentativeSurname1.Text.Trim()) ||
                        String.IsNullOrEmpty(TextBoxRepresentativeSurname2.Text.Trim()) || String.IsNullOrEmpty(TextBoxRepresentativeDNI.Text.Trim()))
                    {

                        Message1.Message = "El postulante es menor de edad o tiene carnet de extranjería, registre los datos del representante.";
                        Message1.Type = Constant.MESSAGE_ERROR;
                        return;
                    }
                }

                Interview newInterview = new Interview();

                int interviewId = 1;
                if (db.Interviews.Count() > 0)
                {
                    interviewId = (from u in db.Interviews select u.IdInterview).Max() + 1;
                }
                newInterview.IdInterview = interviewId;
                newInterview.PromoterName = TextBoxPromoterName.Text;
                newInterview.PromoterSurname1 = TextBoxPromoterSurname1.Text;
                newInterview.PromoterSurname2 = TextBoxPromoterSurname2.Text;
                newInterview.PromoterDocumentType = DropDownListPromoterTypeDocument.SelectedValue;
                newInterview.PromoterRUC = TextBoxPromoterRUC.Text;
                newInterview.PromoterDNI = TextBoxPromoterDNI.Text;
                newInterview.PromoterDateBirth = dateBirth;

                newInterview.Type = Convert.ToChar(DropDownListType.SelectedValue);

                newInterview.RepresentativeName = TextBoxRepresentativeName.Text;
                newInterview.RepresentativeSurname1 = TextBoxRepresentativeSurname1.Text;
                newInterview.RepresentativeSurname2 = TextBoxRepresentativeSurname2.Text;
                newInterview.RepresentativeDocumentType = DropDownListRepresentativeTypeDocument.SelectedValue;
                newInterview.RepresentativeRUC = TextBoxRepresentativeRUC.Text;
                newInterview.RepresentativeDNI = TextBoxRepresentativeDNI.Text;

                newInterview.Email = TextBoxEmail.Text;
                newInterview.UniversityId = int.Parse(DropDownListUniversity.SelectedValue);
                newInterview.UniversityOther = TextBoxUniversityOther.Text;

                newInterview.FacultyId = int.Parse(DropDownListFaculty.SelectedValue);
                newInterview.CollegeId = int.Parse(DropDownListCollege.SelectedValue);
                newInterview.FacultyCollegeOther = TextBoxFacultyCollegeOther.Text;

                newInterview.Cycle = TextBoxCycle.Text;
                newInterview.Via = DropDownListVia.SelectedValue;
                newInterview.Address = TextBoxAddress.Text;
                newInterview.DistrictId = int.Parse(DropDownListDistrict.SelectedValue);

                newInterview.HomePhone = TextBoxHomePhone.Text;
                newInterview.CellPhone = TextBoxCellPhone.Text;

                newInterview.InterviewDate = DateTime.Parse(TextBoxInterviewDate.Text + " " + DropDownListInterviewTime.SelectedValue, culture, System.Globalization.DateTimeStyles.AssumeLocal);

                newInterview.Observation = TextBoxObservation.Text;

                newInterview.Mode = "UNIV";
                newInterview.Gender = DropDownListGender.SelectedValue;

                newInterview.Training = false;
                newInterview.Hired = false;

                newInterview.RegisterDate = DateTime.Now;

                if (base.UserSystem.RoleId == 2)
                {
                    newInterview.LiderId = base.UserSystem.Id;
                    if (base.UserSystem.ParentId.HasValue)
                    {
                        newInterview.CoordinatorId = base.UserSystem.ParentId;
                    }
                }
                if (base.UserSystem.RoleId == 3)
                {
                    newInterview.CoordinatorId = base.UserSystem.Id;
                }

                base.db.Interviews.InsertOnSubmit(newInterview);

                db.SubmitChanges();

                ClearData();

                Message1.Message = "Los datos se registraron satisfactoriamente";
                Message1.Type = Constant.MESSAGE_OK;

            }
            catch (Exception ex)
            {

            }


        }
        else
        {
            if (interviewPromoter != null)
            {
                Message1.Message = "El DNI del promotor ya ha sido registrado.";
                Message1.Type = Constant.MESSAGE_ERROR;
            }
            if (interviewRepresentative != null)
            {
                Message1.Message = "El DNI del representante ya ha sido registrado.";
                Message1.Type = Constant.MESSAGE_ERROR;
            }
        }
    }
    protected void DropDownListPromoterTypeDocument_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownListPromoterTypeDocument.SelectedValue.Equals("CE"))
        {
            TextBoxPromoterDNI.MaxLength = 9;
        }
        else
        {
            TextBoxPromoterDNI.MaxLength = 8;
        }
    }
    protected void DropDownListRepresentativeTypeDocument_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownListRepresentativeTypeDocument.SelectedValue.Equals("CE"))
        {
            TextBoxRepresentativeDNI.MaxLength = 9;
        }
        else
        {
            TextBoxRepresentativeDNI.MaxLength = 8;
        }
    }
    
    private void ClearData()
    {
        TextBoxPromoterName.Text = String.Empty;
        TextBoxPromoterSurname1.Text = String.Empty;
        TextBoxPromoterSurname2.Text = String.Empty;
        DropDownListPromoterTypeDocument.SelectedIndex = 0;
        TextBoxPromoterRUC.Text = String.Empty;
        TextBoxPromoterDNI.Text = String.Empty;
        TextBoxDateBirth.Text = String.Empty;

        TextBoxRepresentativeName.Text = String.Empty;
        TextBoxRepresentativeSurname1.Text = String.Empty;
        TextBoxRepresentativeSurname2.Text = String.Empty;
        DropDownListRepresentativeTypeDocument.SelectedIndex = 0;
        TextBoxRepresentativeRUC.Text = String.Empty;
        TextBoxRepresentativeDNI.Text = String.Empty;

        TextBoxEmail.Text = String.Empty;
        DropDownListUniversity.SelectedIndex = 0;
        TextBoxUniversityOther.Text = String.Empty;

        DropDownListFaculty.SelectedIndex = 0;
        DropDownListCollege.SelectedIndex = 0;
        TextBoxFacultyCollegeOther.Text = String.Empty;

        TextBoxCycle.Text = String.Empty;
        DropDownListVia.SelectedIndex = 0;
        TextBoxAddress.Text = String.Empty;
        DropDownListDistrict.SelectedIndex = 0;

        TextBoxHomePhone.Text = String.Empty;
        TextBoxCellPhone.Text = String.Empty;

        TextBoxInterviewDate.Text = String.Empty;
        DropDownListInterviewTime.SelectedIndex = 0;
        TextBoxObservation.Text = String.Empty;

    }
}
